<?php

namespace app\admin\controller\project;

use app\common\controller\Backend;

/**
 * 服务人员
 *
 * @icon fa fa-circle-o
 */
class Projectstaff extends Backend
{

    /**
     * ProjectStaff模型对象
     * @var \app\admin\model\project\ProjectStaff
     */
    protected $model = null;
    protected $media_model = null;
    protected $apply_model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\project\ProjectStaff;
        $this->media_model = new \app\admin\model\project\ProjectMedia;
        $this->apply_model = new \app\admin\model\project\ProjectApply;
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','user_id','nickname','realname','gender','age','education','major','title','mobile','province','city','district','image','status','createtime']);
                $row->visible(['user']);
				$row->getRelation('user')->visible(['nickname','avatar']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


    public function detail($ids = null)
    {
        $row = $this->model->get($ids);

        $this->view->assign("row", $row);

        $cert_list = $this->media_model->where(['staff_id'=>$row->id,'type'=>0])->select();
        $image_list = $this->media_model->where(['staff_id'=>$row->id,'type'=>1])->select();
        $video_list = $this->media_model->where(['staff_id'=>$row->id,'type'=>2])->select();
        $this->view->assign("cert_list", $cert_list);
        $this->view->assign("image_list", $image_list);
        $this->view->assign("video_list", $video_list);
        $project_apply_list = $this->apply_model->where(['staff_id'=>$row->id])->select();
        foreach ($project_apply_list as $k=>$v){
            //dump($this->getWorkWeek($v['work_day']));die;
            $project_apply_list[$k]['work_day'] = $this->getWorkWeek($v['work_day']);
        }
        $this->view->assign("project_apply_list", $project_apply_list);
        return $this->view->fetch();
    }

    //转换店铺的营业日期
    public function getWorkWeek($week)
    {
        /*$week = explode(',',$week);
        $week_list = [0,1,2,3,4,5,6];

        $res = $this->hasDuplicates($week,$week_list);
        $week_list = $this->getWeek();
        if ($res){

            $work_week = [];
            foreach ($week as $k=>$v){
                array_push($work_week,$week_list[$v]);
            }
            return $work_week;
        } else {
            return '周一至周日';
        }*/
        $work_day_arr = explode(',',$week);

        usort($work_day_arr, function($a, $b) {
            return $a - $b; // 升序
        });

        $star = $this->getWeek()[$work_day_arr[0]];
        $end = $this->getWeek()[end($work_day_arr)];

        return $star.'到'.$end;
    }

    public function hasDuplicates($array1, $array2) {
        $diff = array_diff(array_merge($array1, $array2), array_intersect($array1, $array2));
        return !empty($diff);
    }

    public function getWeek()
    {
        return [1=>'周一',2=>'周二',3=>'周三',4=>'周四',5=>'周五',6=>'周六',7=>'周日'];
    }

    public function pass($ids = null)
    {
        $row = $this->model->get($ids);

        $row->status=1;
        $row->audit_time = time();
        if ($row->save()){
            $this->success('审核成功');
        } else {
            $this->error('审核失败');
        }
    }

    public function refuse($ids = null)
    {
        $row = $this->model->get($ids);
        $row->status = 2;
        $row->audit_time = time();
        if ($row->save()){

            $this->success('拒绝成功');
        } else {
            $this->error('拒绝失败');
        }
    }
}
